package com.zhentao.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhentao.mapper.RoleMapper;
import com.zhentao.pojo.Role;
import com.zhentao.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/role")
public class RoleController {
    @Resource
    RoleMapper roleMapper;

//    分配角色 查询所有角色
    @GetMapping("all")
    public Result getAllRoles(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size) {
        // 创建分页对象
        Page<Role> pageParam = new Page<>(page, size);

        // 执行分页查询
        Page<Role> rolePage = roleMapper.selectPage(pageParam, null);

        // 返回分页结果
        Map<String, Object> result = new HashMap<>();
        result.put("roles", rolePage.getRecords());
        result.put("total", rolePage.getTotal());
        result.put("pages", rolePage.getPages());
        result.put("current", rolePage.getCurrent());
        result.put("size", rolePage.getSize());

        return Result.OK(result);
    }
}
